package co2.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.DualListModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.pojos.MstUser;
import co2.pojos.PaketMotivasi;
import co2.pojos.PaketSoal;
import co2.pojos.ViewUser;

@ManagedBean
@ViewScoped
public class AssignmentMotivasiBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<PaketMotivasi> listPS;
	private PaketMotivasi ps;
	private String psId;
	private DualListModel<MstUser> assignUser;
	private List<ViewUser> listUser;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	
	@PostConstruct
	public void postCon(){ 
		listPS = dao.getAllPaketMotivasi();
		if(listPS!=null&&listPS.size()>0){
			psId = ((PaketMotivasi) listPS.get(0)).getId();
		}
		initPicklist();
	}
	
	public void initPicklist(){
		List<MstUser> source = new ArrayList<MstUser>();
		List<MstUser> des = new ArrayList<MstUser>();
		
		source = dao.getMstUserByAssignMotivasi(null);
		if(psId!=null){
			des = dao.getMstUserByAssignMotivasi(psId);
		}
		
		assignUser = new DualListModel<MstUser>(source,des);
		listUser = dao.getAllForAssignmentViewMotivasi();
	}
	
	public void submit(){
		List<MstUser> des = assignUser.getTarget();
		List<MstUser> src = assignUser.getSource();
		if(des!=null&&des.size()>0){
			for(MstUser m:des){
				m.setAssignedMotivasi(psId);
				try{
					dao.update(m);
				}catch(Exception e){
					System.out.println("=================================> ERROR WHEN ASSIGN TO USER");
					e.printStackTrace();
				}
			}
			if(src!=null&&src.size()>0){
				for(MstUser u:src){
					u.setAssignedMotivasi(null);
					try{
						dao.update(u);
					}catch(Exception eu){
						System.out.println("=============================> ERROR WHEN UNASSIGN USER");
						eu.printStackTrace();
					}
				}
			}
			listUser = dao.getAllForAssignmentViewMotivasi();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Success",""));
		}else{
//			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"no user to assign",""));
			if(src!=null&&src.size()>0){
				for(MstUser u:src){
					u.setAssignedMotivasi(null);
					try{
						dao.update(u);
					}catch(Exception eu){
						System.out.println("=============================> ERROR WHEN UNASSIGN USER");
						eu.printStackTrace();
					}
				}
			}
			listUser = dao.getAllForAssignmentViewMotivasi();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Success",""));
		}
	}

	public String getPsId() {
		return psId;
	}

	public void setPsId(String psId) {
		this.psId = psId;
	}

	public DualListModel<MstUser> getAssignUser() {
		return assignUser;
	}

	public void setAssignUser(DualListModel<MstUser> assignUser) {
		this.assignUser = assignUser;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	public List<ViewUser> getListUser() {
		return listUser;
	}

	public void setListUser(List<ViewUser> listUser) {
		this.listUser = listUser;
	}

	public List<PaketMotivasi> getListPS() {
		return listPS;
	}

	public void setListPS(List<PaketMotivasi> listPS) {
		this.listPS = listPS;
	}

	public PaketMotivasi getPs() {
		return ps;
	}

	public void setPs(PaketMotivasi ps) {
		this.ps = ps;
	}

}